home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / SAT 2.4.0 / SAT / Demo ƒ / Bricks demo ƒ / Bricks.p < prev    next >
Encoding:
Text File  |  1997-02-08  |  6.4 KB  |  239 lines  |  [TEXT/PJMM]

  1. {Bricks}
  2. {}
  3. {Demo program for SAT 2.3, by Ingemar Ragnemalm november 1994}
  4. {}
  5. {The purpose of this demo is to test and demonstrate the support for what I call "resting" sprites.}
  6. {If a sprite neither moves, changes face, overlaps a changing sprite nor changes its place in the}
  7. {sprite list, then SAT can avoid drawing it.}
  8. {}
  9. {For programs where all sprites change in every frame (which is the case in many arcade games)}
  10. {then this is not of any interest at all, and will just slow things down. For other programs, for}
  11. {example board games, Tetris-style games etc, this technique is very useful.}
  12. {}
  13. {For the moment (SAT 2.3d3), you get the new system by calling SATRun2 instead of SATRun. The name}
  14. {of SATRun2 is likely to change in the future.}
  15. {}
  16. {When you start Bricks, the sprites will not be initialied in order, so since VPositionSort}
  17. {is active, they will sort during the first frames. This slows the program down for the first}
  18. {few seconds. If this is a problem, it can be avoided by either turning off sorting or}
  19. {creating the sprite in the proper places from the start. Hint: try BucketSortV from}
  20. {SortingUtils.p!}
  21. {}
  22. {Bug note: When using "fast graphics", the cursor will cause some "mouse droppings". This is best avoided}
  23. {by hiding the cursor and repllacing it with a SAT sprite. The "proper" way to avoid the problem is to}
  24. {call ShieldCursor, but that will make the cursor flicker a lot.}
  25.  
  26. program Bricks;
  27.     uses
  28. {$ifc UNDEFINED THINK_PASCAL}
  29.         Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit, TextEdit, Traps,{}
  30.         Memory, SegLoad, Scrap, ToolUtils, OSUtils, Menus, Resources, StandardFile,{}
  31.         GestaltEqu, Files, Errors, Devices, 
  32. {$elsec}
  33.         InterfacesUI, 
  34. {$endc}
  35.         SAT;
  36.  
  37.     const
  38.         kNumBricks = 30;
  39.     var
  40.         brickFace: array[0..kNumBricks] of FacePtr;
  41.         myEvent: EventRecord;
  42.         whichWindow: WindowPtr;
  43.         appleMenu, fileMenu: MenuHandle;
  44.         gUseStagger, gDone, gUseFast, gAllowBackground: Boolean;
  45.         i: integer;
  46.         sp, found: SpritePtr;
  47.         theSelection: Longint;
  48.         where: Point;
  49.         theKey: Char;
  50.         whichPart: Integer;
  51.         hasEvent: Boolean;
  52.  
  53.     procedure InitBricks;
  54.         var
  55.             i: integer;
  56.     begin
  57.         for i := 0 to kNumBricks - 1 do
  58.             brickFace[i] := SATGetFace(i + 128);
  59.     end;
  60.  
  61.     procedure HandleRestingBrick (me: SpritePtr);
  62.     begin
  63.     end;
  64.  
  65.     procedure HandleFollowMouse (me: SpritePtr);
  66.         var
  67.             mousePos: Point;
  68.     begin
  69.         if Button then
  70.             begin
  71.                 GetMouse(mousePos);
  72.                 me^.position.h := me^.position.h + mousePos.h - me^.speed.h;
  73.                 me^.position.v := me^.position.v + mousePos.v - me^.speed.v;
  74.                 me^.speed := mousePos;
  75.             end
  76.         else
  77.             me^.task := @HandleRestingBrick;
  78.     end;
  79.  
  80.     procedure SetupBrick (me: SpritePtr);
  81.     begin
  82.         me^.task := @HandleRestingBrick;
  83.         me^.face := brickFace[me^.kind];
  84.     end;
  85.  
  86.     procedure SynchMenus;
  87.     begin
  88.         CheckItem(fileMenu, 1, not gUseStagger);
  89.         CheckItem(fileMenu, 2, gUseStagger);
  90.         CheckItem(fileMenu, 3, gUseFast);
  91.         CheckItem(fileMenu, 4, gAllowBackground);
  92.     end;
  93.  
  94.     procedure MenuSelection (theSelection: Longint);
  95.         var
  96.             name: Str255;
  97.             saveport: GrafPtr;
  98.     begin
  99.         case HiWord(theSelection) of
  100.             128: 
  101.                 begin
  102.                     if LoWord(theSelection) = 1 then
  103.                         SATReportStr('Experimental SAT demo program. Resting sprites allow good speed with many sprites!')
  104.                     else
  105.                         begin
  106.                             GetPort(saveport);
  107.                             GetMenuItemText(appleMenu, LoWord(theSelection), name);    (* get name *)
  108.                             if OpenDeskAcc(name) = 0 then (* run the desk accessory *)
  109.                                 ;
  110.                             SetPort(saveport);
  111.                         end;
  112.                 end;
  113.             129: 
  114.                 case LoWord(theSelection) of
  115.                     1: 
  116.                         gUseStagger := false;
  117.                     2: 
  118.                         gUseStagger := true;
  119.                     3: 
  120.                         gUseFast := not gUseFast;
  121.                     4: 
  122.                         gAllowBackground := not gAllowBackground;
  123.                     6: 
  124.                         gDone := true;
  125.                 end; {case MenuSelect}
  126.             otherwise
  127.         end;
  128.         SynchMenus;
  129.         HiLiteMenu(0);
  130.     end;
  131.  
  132. begin
  133. {CodeWarrior needs inits here}
  134. {$IFC UNDEFINED THINK_PASCAL}
  135.     SATInitToolbox;
  136. {$ENDC}
  137.  
  138.     InitBricks;
  139.  
  140.     SATInit(128, 129, 512, 342);
  141.     SATSetPortScreen;
  142.     gUseStagger := true;
  143.     gAllowBackground := true;
  144.     gUseFast := false;
  145.  
  146.     for i := 0 to kNumBricks - 1 do
  147.         sp := SATNewSprite(i, SATRand(gSAT.offSizeH - 48), SATRand(gSAT.offSizeV - 64), @SetupBrick);
  148.     for i := 0 to kNumBricks - 1 do
  149.         sp := SATNewSprite(i, SATRand(gSAT.offSizeH - 48), SATRand(gSAT.offSizeV - 64), @SetupBrick);
  150.  
  151.     appleMenu := NewMenu(128, stringof(char($14)));
  152.     AppendMenu(appleMenu, 'About Bricks demo…;(-');
  153.     AppendResMenu(appleMenu, 'DRVR');
  154.     InsertMenu(appleMenu, 0);            { put apple menu at end of menu bar }
  155.  
  156.     fileMenu := NewMenu(129, 'File');
  157.     AppendMenu(fileMenu, 'RunSAT/1;RunSAT2/2;Fast graphics/F;Allow background tasks;(-;Quit/Q');
  158.     InsertMenu(fileMenu, 0);            { put file menu at end of menu bar }
  159.     DrawMenuBar;
  160.     SynchMenus;
  161.  
  162.     InitCursor;
  163.  
  164.     repeat
  165. {if gUseFast then}
  166. {ShieldCursor(gSAT.bounds, Point(0));}
  167.         if gUseStagger then
  168.             SATRun2(gUseFast)
  169.         else
  170.             SATRun(gUseFast);
  171. {if gUseFast then}
  172. {ShowCursor;}
  173.  
  174.         if gAllowBackground then
  175.             begin
  176.                 SystemTask;
  177.                 hasEvent := GetNextEvent(everyEvent, myEvent)
  178.             end
  179.         else
  180.             hasEvent := GetOSEvent(everyEvent, myEvent);        {or perhaps mDownMask + updateMask + keyDownMask}
  181.         if hasEvent then
  182.             case myEvent.what of
  183.                 updateEvt: 
  184.                     if WindowPtr(myEvent.message) = gSAT.wind.port then
  185.                         begin
  186.                             BeginUpdate(gSAT.wind.port);
  187.                             SATRedraw;
  188.                             EndUpdate(gSAT.wind.port);
  189.                         end;
  190.                 keyDown: 
  191.                     begin
  192.                         theKey := char(BitAnd(myEvent.message, charCodeMask));
  193.                         if (BitAnd(myEvent.modifiers, cmdKey) <> 0) then
  194.                             MenuSelection(MenuKey(theKey))
  195.                         else
  196. {DoKey(theKey, theEvent.modifiers)}
  197.                             ;
  198.                     end;
  199.                 mouseDown: 
  200.                     begin
  201.                         whichPart := FindWindow(myEvent.where, whichWindow);
  202.                         case whichPart of
  203.                             inMenuBar: 
  204.                                 begin
  205.                                     theSelection := MenuSelect(myEvent.where);
  206.                                     MenuSelection(theSelection);
  207.                                 end;
  208.                             inSysWindow: 
  209.                                 SystemClick(myEvent, whichWindow);
  210.                             inContent: 
  211.                                 begin
  212.                                     found := nil;
  213.                                     sp := gSAT.sRoot;
  214.                                     GetMouse(where);
  215.                                     while sp <> nil do
  216.                                         begin
  217.                                             if PtInRect(where, sp^.r) then
  218.                                                 found := sp;
  219.                                             sp := sp^.next;
  220.                                         end;
  221.                                     if found <> nil then
  222.                                         if myEvent.modifiers <> 0 then
  223.                                             found^.task := nil
  224.                                         else
  225.                                             begin
  226.                                                 found^.task := @HandleFollowMouse;
  227.                                                 found^.speed := where; {not speed, but storage for old mouse position}
  228.                                             end;
  229.                                 end;
  230.                             otherwise
  231.                         end; {case whichPart}
  232.                     end;
  233.  
  234.                 otherwise
  235.             end;{case myEvent.what}
  236.     until gDone;
  237.  
  238.     SATSoundShutup;
  239. end.